import 'package:flutter/foundation.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:kxy_flutter_app/helper/http.dart';
import 'package:kxy_flutter_app/model/exam.dart';
import 'package:kxy_flutter_app/model/exam_category.dart';
import 'package:kxy_flutter_app/model/exam_rank.dart';

class ExamRankService {
  String userId;
  ExamRankService({@required this.userId});

  Future<ExamRank> getRank(String cateId, {String examId}) async {
    var res = await Http.instance.post(
      '/exam-app/rank/examRank',
      params: {
        'userId': userId,
        'cateId': cateId,
        'examId': examId,
      },
    );

    if (res.code == 200) {
      return ExamRank.fromJson(res.data);
    } else {
      Fluttertoast.showToast(msg: res.msg);
      return null;
    }
  }

  Future<List<ExamProfile>> getExamList(String cateId) async {
    var res = await Http.instance.post(
      '/exam-app/rank/examList',
      params: {'cateId': cateId},
    );
    if (res.code == 200) {
      return (res.data['examList'] as List).map((e) => ExamProfile.fromJson(e)).toList();
    } else {
      Fluttertoast.showToast(msg: res.msg);
      return [];
    }
  }

  Future<List<ExamCategory>> getExamCateList() async {
    var res = await Http.instance.post('/exam-app/exam/cate/list');
    if (res.code == 200) {
      return (res.data as List).map((e) => ExamCategory.fromJson(e)).where((x) => x.type == 1).toList();
    } else {
      Fluttertoast.showToast(msg: res.msg);
      return [];
    }
  }
}
